// pages/ceshi/ceshi.js
let QRCode = require("../../util/qrcode.js");
let app=getApp();
let jumpurl=app.jumpurl.url;
let access_token;
Page({

 
  data: {
      erweima:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
   

    console.log("onload");
    // if (wx.getStorageSync('access_token')){
     
    //   access_token = wx.getStorageSync('access_token');
    //   console.log(access_token);
    // }else{
      wx.request({
        url: jumpurl + 'token/index',
        data: {},
        method: 'GET',
        header: {
          'Content-type': 'application/json'
        },
        success: function (res) {
          console.log("---------access_token-----------");
          console.log(res);
          var access_token = res.data;
          wx.setStorageSync('access_token', access_token)

          //生成小程序二维码
          wx.request({
            url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=' + access_token,
            data: {
              "path": "/pages/setter/setter",
              "width": "100"
            },
            method: "POST",

            success: function (res) {
              console.log("**************success")
              console.log(res);
              that.setData({
                erweima: res.data
              })
            },
            fail: function (err) {
              console.log(err);
            }
          })
        },
        fail: function (err) {
          console.log("err" + err);
        }

      })
    // }
      console.log(access_token);
    qrcode = new QRCode('canvas', {
      text: "code="+that.data.erweima,
      width: 150,
      height: 150,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
   
  },

  tapHandler: function (e) {
    console.log("123");
    qrcode.makeCode(e.target.dataset.code);  //用元素对应的code更新二维码
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})